/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import utils.LogUtils;

/**
 *
 * @author Valentina
 */
public final class MyDBDriver {
    
    Logger logger = LogUtils.getLogger(this.getClass().getName());
    
    Connection conn = null;

    private static MyDBDriver mdbd = null;

    /**
     * Method getMyDBDriver
     * Create Data Base Driver
     * 
     */
    public static MyDBDriver getMyDBDriver() {

        if (mdbd == null) {
            mdbd = new MyDBDriver();
        }

        return mdbd;
    }

    /**
     * Method MyDBDriver
     * Constructor
     */
    private MyDBDriver() {
        createConnection();
    }

    /**
     * Method createConnection
     * Create connection DataSource
     */
    public void createConnection() {
        try {

            javax.naming.Context ctx = new javax.naming.InitialContext();

            javax.naming.Context envctx = (javax.naming.Context) ctx.lookup("java:comp/env");
            
            DataSource ds = (DataSource) envctx.lookup("jdbc/PL2");

            try {
                conn = ds.getConnection();
            } catch (SQLException e) {
                //Eroare creare conexiune
                logger.error(e.getNextException().getMessage());
            }
        } catch (Exception ex) {
            //Eroare nu a fost gasit datasource
            logger.error(ex.getMessage());
        }
        
    }

    /**
     * Method getConn
     * Create and return connection
     */
    public Connection getConn() {
        this.createConnection();
        return conn;
    }

}